package br.net.atasacramental.atividade.dao;
import br.net.atasacramental.atividade.model.Evento;
import br.net.atasacramental.atividade.model.EventoStatus;
import br.net.atasacramental.atividade.model.EventoView;
import br.net.atasacramental.util.ConexaoDB;
import br.net.atasacramental.util.Paginacao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author alexandre.araujo
 */
public class EventoDAO{
	private String[][] ordemP = {{"idEvento", "desc"}};
    @SuppressWarnings("unchecked")
	public List<Evento> get(){
    	return (List<Evento>) ConexaoDB.get(Evento.class,true,1,100,null,ordemP);
    }
    public Evento getByIdEvento(int idEvento){
    	return (Evento) ConexaoDB.getById(Evento.class,idEvento);
    } 
    public EventoView getByIdEventoView(int idEvento){
    	return (EventoView) ConexaoDB.getById(EventoView.class,idEvento);
    } 
    
    @SuppressWarnings("unchecked")
	public List<Evento> getPaginado(Paginacao pag){
     	Map<String,Object> params = new HashMap<String, Object>();     
    	if(pag.getOrderBy()!=null){
    		String[][] ordemP2 = {{pag.getOrderBy(),pag.getOrder()}};
    		ordemP = ordemP2;
    	}
    	if(!(pag.getFilterValue()==(null)||pag.getFilterBy().equals("idEvento"))){
    		params.put(pag.getFilterBy(), pag.getFilterValue());
    	}
    	return (List<Evento>) ConexaoDB.get(Evento.class,false,pag.getInitial(),pag.getMax(),params,ordemP);
    }    
    @SuppressWarnings("unchecked")
	public List<Evento> getByDescricao(String descricao){
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("descricao", descricao);
        return (List<Evento>) ConexaoDB.get(Evento.class,true,1,100,params,ordemP);
    }
    
    @SuppressWarnings("unchecked")
	public List<Evento> getByDetalhes(String detalhes){
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("detalhes", detalhes);
        return (List<Evento>) ConexaoDB.get(Evento.class,true,1,100,params,ordemP);
    }
    @SuppressWarnings("unchecked")
	public List<Evento> getByIdOrganizacao(int idOrganizacao){
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("idOrganizacao", idOrganizacao);
        return (List<Evento>) ConexaoDB.get(Evento.class,true,1,100,params,ordemP);
    }
    @SuppressWarnings("unchecked")
	public List<EventoView> getByLogin(String login){
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("loginUsuario", login);
        return (List<EventoView>) ConexaoDB.get(EventoView.class,true,1,100,params,ordemP);
    }
    @SuppressWarnings("unchecked")
	public List<EventoView> getByLogin(String login,int pidStatus){
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("loginUsuario", login);
     	params.put("idStatus", pidStatus);
        return (List<EventoView>) ConexaoDB.get(EventoView.class,true,1,100,params,ordemP);
    }
    @SuppressWarnings("unchecked")
    public List<Evento> getByIdOrganizacao(int idOrganizacao,String editavel){
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("idOrganizacao", idOrganizacao);     	
		List<Evento> eventos = (List<Evento>) ConexaoDB.get(Evento.class,true,1,100,params,ordemP);
     	if(editavel.equals("S")){
	     	for(int x = 0;x<eventos.size();x++){
	     		eventos.get(x).setSnEditavel(editavel);
	     	}    
     	}
        return eventos;
    }
    @SuppressWarnings("unchecked")
	public List<Evento> getByIdOrganizacaoEidStatusEidTrimestre(int idOrganizacao,int idStatus,String editavel,int idTrimestre){
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("idOrganizacao", idOrganizacao);
     	params.put("idStatus", idStatus);
     	params.put("idTrimestre", idTrimestre);
     	List<Evento> eventos = (List<Evento>) ConexaoDB.get(Evento.class,true,1,100,params,ordemP);
     	if(editavel.equals("S")){
	     	for(int x = 0;x<eventos.size();x++){
	     		eventos.get(x).setSnEditavel(editavel);
	     	}    
     	}
        return eventos;
    }
    public List<Evento> getByMomento(Date Dta) {
    	return this.getByPeriodo(Dta, Dta);
    }
    
    @SuppressWarnings("unchecked")
	public List<Evento> getAprovadasELiberadasByPeriodo(Date dtI,Date dtF) {
    	dtF.setTime(dtF.getTime()+86399999L);
    	String dataz = "yyyy-MM-dd";
        SimpleDateFormat formatas = new SimpleDateFormat(dataz );
        String dataI = formatas.format(dtI);
        String dataF = formatas.format(dtF);        
        return (List<Evento>) ConexaoDB.listar("From Evento e where e.idStatus in ("+EventoStatus.APROVADA+","+EventoStatus.LIBERADA+")   and e.momento between '"+dataI+"' and '"+dataF+"' order by e.momento asc");
    }
    @SuppressWarnings("unchecked")    
    public List<Evento> getByIdTrimestreExcludente(Evento e) {
    	Date dtI=e.getMomento();
    	Date dtF=e.getMomento();
    	dtF.setTime(dtF.getTime()+86399999L);
        String dataz = "yyyy-MM-dd";
        SimpleDateFormat formatas = new SimpleDateFormat(dataz);
        String dataI = formatas.format(dtI);
        String dataF = formatas.format(dtF);   	
        return (List<Evento>) ConexaoDB.listar("From Evento e where e.idTrimestre = "+e.getIdTrimestre()+" and e.idEvento <> "+e.getIdEvento()+" and e.momento between '"+dataI+"' and '"+dataF+"' order by e.momento asc");
    }
    @SuppressWarnings("unchecked")    
    public List<Evento> getByPeriodo(Date DtaI,Date DtaF){  	
    	DtaF.setTime(DtaF.getTime()+86399999L);
		return (List<Evento>) ConexaoDB.getBetween(Evento.class,true,1,100,"momento",DtaI,DtaF,ordemP);
    }
    @SuppressWarnings("unchecked")    
    public List<Evento> getByHora(String hora) {
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("hora", hora);
        return (List<Evento>) ConexaoDB.get(Evento.class,true,1,100,params,ordemP);
    }
    @SuppressWarnings("unchecked")    
    public List<Evento> getByIdResponsavel(int idResponsavel) {
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("idResponsavel", idResponsavel);
        return (List<Evento>) ConexaoDB.get(Evento.class,true,1,100,params,ordemP);
    }
    @SuppressWarnings("unchecked")    
    public List<Evento> getByOrcamento(Integer orcamento) {
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("orcamento", orcamento);
        return (List<Evento>) ConexaoDB.get(Evento.class,true,1,100,params,ordemP);
    }
    @SuppressWarnings("unchecked")    
    public List<Evento> getByProposito(String proposito) {
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("proposito", proposito);
        return (List<Evento>) ConexaoDB.get(Evento.class,true,1,100,params,ordemP);
    }
    @SuppressWarnings("unchecked")
    public List<Evento> getByIdStatus(int idStatus) {
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("idStatus", idStatus);
        return (List<Evento>) ConexaoDB.get(Evento.class,true,1,100,params,ordemP);
    }
    @SuppressWarnings("unchecked")
    public List<Evento> getByIdTrimestre(int idTrimestre) {
     	Map<String,Object> params = new HashMap<String, Object>();
     	params.put("idTrimestre", idTrimestre);
     	String[][] ordemData = {{"momento", "desc"}};
     	List<Evento> lstevts = (List<Evento>) ConexaoDB.get(Evento.class,true,1,100,params,ordemData);
        
     	//ConexaoDB.removeFromSession(lstevts);
     	//ConexaoDB.getSession().clear();
     	return lstevts;
    }
    
    private void formatar(Evento e){
    	if(e.getDsObservacao()==null){
    		e.setDsObservacao("");
    	}
    	if(e.getVestuario()==null){
    		e.setVestuario("");
    	}
    }
    public boolean insert(Evento e){
    	this.formatar(e);
        return ConexaoDB.insert(e);
    }
    public boolean update(Evento e){    	
    	EventoView evt = this.getByIdEventoView(e.getIdEvento());
    	if(evt != null){
    		ConexaoDB.removeFromSession(evt);
    	}    	
    	this.formatar(e);
        return ConexaoDB.update(e);
    }
    public boolean delete(Evento e){
        return ConexaoDB.delete(e);
    }
};
